package xing.service.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xing.application.service.page.TemplateService;
import xing.application.service.page.dto.MyTemplate;
import xing.domain.page.PageTemplate;
import xing.domain.page.annotation.Access;
import xing.domain.page.enums.AccessType;
import xing.infrastructure.data.CallResult;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

@Api(value = "模板相关接口")
@RestController
@RequestMapping("/template")
public class TemplateController {
    @Autowired
    TemplateService templateService;

    @ApiOperation(value="获取企业所有模板", notes="")
    @ApiImplicitParam(name = "corpId", value = "公司id", required = true, dataType = "String", paramType = "path" )
    @RequestMapping(value = "/list/{corpId}",method = RequestMethod.POST)
    public List<PageTemplate> getTemplateList(@PathVariable("corpId") String corpId){
        List<PageTemplate> res=templateService.getTemplateList(corpId);
        return res;
    }

    @ApiOperation(value="获取企业模板(按类型)", notes="")
    @ApiImplicitParams({@ApiImplicitParam(name = "corpId", value = "公司id", required = true, dataType = "String", paramType = "path" ),
    @ApiImplicitParam(name = "type", value = "模板类型 1：官方预定义模板: 2：快照3:当前使用4：自定义模板(发布过的)", required = true, dataType = "int", paramType = "path" )})
    @RequestMapping(value = "/lists/{corpId}/{type}",method = RequestMethod.POST)
    public List<PageTemplate> getTemplateList(@PathVariable("corpId") String corpId,@PathVariable("type") int templatetype){
        List<PageTemplate> res=templateService.getTemplateList(corpId,templatetype);
        return res;
    }

    @ApiOperation(value="获取我的模板列表", notes="")
    @ApiImplicitParam(name = "corpId", value = "公司id", required = true, dataType = "String", paramType = "path" )
    @RequestMapping(value = "/mylist/{corpId}",method = RequestMethod.POST)
    public List<MyTemplate> getMyTemplateList(@PathVariable("corpId") String corpId){
        List<MyTemplate> res=templateService.getMyTemplateList(corpId);
        return res;
    }

    @ApiOperation(value="获取官方模板列表", notes="")
    @RequestMapping(value = "/official",method = RequestMethod.POST)
    public List<PageTemplate> getOfficalTemplateList(){
        List<PageTemplate> res=templateService.getOfficialTemplate();
        return res;
    }

    @ApiOperation(value="获取模板详细信息", notes="")
    @ApiImplicitParam(name = "id", value = "模板id", required = true, dataType = "lang", paramType = "path" )
    @RequestMapping(value = "/detail/{id}",method = RequestMethod.POST)
    public PageTemplate getTemplateDetail(@PathVariable long id){
        PageTemplate res=templateService.getTemplateDetail(id);//150122467785203L
        return res;
    }

    @ApiOperation(value="获取公司当前模板详细信息", notes="")
    @ApiImplicitParam(name = "corpId", value = "公司code", required = true, dataType = "String", paramType = "path" )
    @RequestMapping(value = "/currentdetail/{corpId}",method = RequestMethod.POST)
    public CallResult getTemplateDetail(@PathVariable String corpId){
        CallResult res=templateService.getCorpCurrentTemplateDetail(corpId);
        return res;
    }

    @Access(accessType = AccessType.ADMIN)
    @ApiOperation(value="模板重命名", notes="")
//    @ApiImplicitParams({@ApiImplicitParam(name = "templateId", value = "模板id", required = true, dataType = "lang", paramType = "query" ),
//            @ApiImplicitParam(name = "name", value = "模板名称", required = true, dataType = "lang", paramType = "query" )})
    @ApiImplicitParam(name = "para", value = "如：{\"templateId\":150252065223705,\"name\":\"123\"}", required = true, dataType = "Map")
    @RequestMapping(value = "/rename",method = RequestMethod.POST)
    public CallResult updateTemplateName(@RequestBody Map<String,Object> para){
        long templateId=(Long)para.get("templateId");
        String name=para.get("name").toString();
        CallResult res=templateService.updateTemplateName(templateId,name);
        return res;
    }

    @Access(accessType = AccessType.ADMIN)
    @ApiOperation(value="删除模板", notes="")
    @ApiImplicitParam(name = "id", value = "模板id", required = true, dataType = "Lang", paramType = "path" )
    @RequestMapping(value = "/del/{corpId}/{id}",method = RequestMethod.POST)
    public CallResult delTemplate(@PathVariable long id,@PathVariable String corpId){
        CallResult r=templateService.deleteTemplate(id,corpId);
        return r;
    }

    @ApiOperation(value="生成模板快照", notes="")
    @ApiImplicitParam(name = "para", value = "如：{\"templateId\":150252065223705,\"corpId\":\"123\"} 从空白模板创建，模板id传0", required = true, dataType = "Map")
    @RequestMapping(value = "/generate",method = RequestMethod.POST)
    public CallResult CreateTemplate(@RequestBody Map<String,Object> para){
        long templateId=Long.parseLong(para.get("templateId").toString());
        String corpId=para.get("corpId").toString();
        CallResult r=templateService.genTemplateSnap(templateId,corpId);
        return r;
    }

    @ApiOperation(value="保存模板缩略图", notes="")
    @ApiImplicitParam(name = "data", value = "如：{\"templateId\":150252065223705,\"base64\":\"\"}", required = true, dataType = "Map" )
    @RequestMapping(value = "/thumb",method = RequestMethod.POST)
    public CallResult SaveThumb(@RequestBody Map<String,Object> data) throws Exception{
        long templateId=Long.parseLong(data.get("templateId").toString());
        String base64=(String)data.get("base64");
        System.out.print("thread:"+Thread.currentThread().getName());
        Future<CallResult> future=templateService.saveThumbFromBase64(templateId, base64);
        //可不等待结果，直接返回
        CallResult r=future.get();
        return r;
//        while (true) {
//            if(future.isDone()){
//                CallResult r=future.get();
//                return r;
//            }
//
//        }
        //CallResult r=future.get();

    }

    @Access(accessType = AccessType.ADMIN)
    @ApiOperation(value="发布模板", notes="")
    @ApiImplicitParam(name = "para", value = "如：{\"templateId\":150252065223705,\"corpId\":\"123\"}", required = true, dataType = "Map")
    @RequestMapping(value = "/release",method = RequestMethod.POST)
    public CallResult releaseTemplate(@RequestBody Map<String,Object> para,HttpServletRequest request){
        long templateId=Long.parseLong(para.get("templateId").toString());
        String corpId=para.get("corpId").toString();
        CallResult r=templateService.releaseTemplate(templateId,corpId);
        return r;
    }

}
